<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Admin extends MY_Controller
{

    private function _error( $url )
    {
        header( "location:$url" );
        exit;
    }

    /**
     * 管理区域
     */
    public function zone( $zoneId = '' )
    {
        if ( !$this->permission->chkPermission( 'zone' ) ) $this->_error( config_item( 'base_url' ) );
        $data = array( );

        if ( $zoneId )
        {
            $selectedZoneData = $this->model_common_getZoneDataById( $zoneId );
            $data['selectedZoneData'] = $selectedZoneData;
        }
        $zoneData = $this->model_common_getZoneData();
        $data['zoneData'] = $zoneData;

        $this->get_common_section( $data );
        $this->load->view( 'admin/zone', $data );
    }

    /**
     * 添加区域
     */
    public function zoneadd()
    {
        if ( !$this->permission->chkPermission( 'zone' ) ) $this->_error( config_item( 'base_url' ) );
        $zoneName = $this->_getZoneData();
        if ( $zoneName )
        {
            $zoneId = $this->model_common_addZone( $zoneName );
            //提示
            $messageData = array(
                'title' => '操作成功',
                'message' => "{$zoneName}已添加，4秒后自动跳转回区域管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'zone', '', false ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['message'] = $messageBox;

            $this->get_common_section( $data );
            $this->load->view( 'admin/zone', $data );
        }
        else
        {
            $this->zone();
        }
    }

    public function zoneedit()
    {
        if ( !$this->permission->chkPermission( 'zone' ) ) $this->_error( config_item( 'base_url' ) );
        $zoneId = intval( $this->input->post( 'id' ) );
        $zoneData = $this->model_common_getZoneDataById( $zoneId );
        if ( empty( $zoneData ) )
        {
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => "4秒后自动跳转回区域管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'zone', '', false ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['message'] = $messageBox;

            $this->get_common_section( $data );
            $this->load->view( 'admin/zone', $data );
        }
        else
        {
            $zoneName = $this->_getZoneData( true );
            if ( $zoneName )
            {
                $zoneId = $this->model_common_updateZoneNameById( $zoneName, $zoneId );
                //提示
                $messageData = array(
                    'title' => '操作成功',
                    'message' => "4秒后自动跳转回区域管理",
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => my_url( 'admin', 'zone', '', false ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data['message'] = $messageBox;

                $this->get_common_section( $data );
                $this->load->view( 'admin/zone', $data );
            }
            else
            {
                $this->zone();
            }
        }
    }

    /**
     * 删除指定的区域ID数据
     * @param int $zoneId 区域ID
     */
    public function zonedelete( $zoneId )
    {
        if ( !$this->permission->chkPermission( 'zone' ) ) $this->_error( config_item( 'base_url' ) );
        $zoneData = $this->model_common_getZoneDataById( $zoneId );
        if ( empty( $zoneData ) )
        {
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => "4秒后自动跳转回区域管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'zone', '', false ),
            );
        }
        else
        {
            $this->model_common_deleteZoneById( $zoneId );
            $messageData = array(
                'title' => '操作成功',
                'message' => "{$zoneName}已删除，4秒后自动跳转回区域管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'zone', '', false ),
            );
        }
        $messageBox = Modules::run( 'messageBox', $messageData );
        $data['message'] = $messageBox;
        $this->get_common_section( $data );
        $this->load->view( 'admin/zone', $data );
    }

    /**
     * 获取提交的地区表单数据
     * @return string
     */
    private function _getZoneData( $isEdit = false )
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        if ( $isEdit )
        {
            $this->form_validation->set_rules( 'zone', '区域', 'required' );
        }
        else
        {
            $this->form_validation->set_rules( 'zone', '区域', 'required|callback_zoneExist' );
        }
        if ( $this->form_validation->run() ) return $this->input->post( 'zone' );
        return false;
    }

    /**
     * 判断某个地区名称是否已经存在
     * @param string $zoneName
     * @return bool
     */
    public function zoneExist( $zoneName )
    {
        $zoneData = $this->model_common_getZoneDataByName( $zoneName );
        if ( $zoneData )
        {
            $this->form_validation->set_message( 'zoneExist', '%s已经存在' );
            return false;
        }
        else
        {
            return true;
        }
    }
    
    /**
     * 判断某个厅室是否已经存在
     * @param string $roomName
     * @return bool
     */
    public function roomExist( $roomName )
    {
        $roomData = $this->model_common_getRoomDataByName( $roomName );
        if ( $roomData )
        {
            $this->form_validation->set_message( 'roomExist', '%s已经存在' );
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * 管理厅室
     */
    public function room( $roomId = '' )
    {
        if ( !$this->permission->chkPermission( 'room' ) ) $this->_error( config_item( 'base_url' ) );
        
        $data = array( );

        if ( $roomId )
        {
            $selectedRoomData = $this->model_common_getRoomDataById( $roomId );
            $data['selectedRoomData'] = $selectedRoomData;
        }
        $roomData = $this->model_common_getRoomData();
        $data['roomData'] = $roomData;

        $this->get_common_section( $data );
        $this->load->view( 'admin/room', $data );
    }
    
    /**
     * 添加厅室
     */
    public function roomadd()
    {
        if ( !$this->permission->chkPermission( 'room' ) ) $this->_error( config_item( 'base_url' ) );
        $roomName = $this->_getRoomData();
        if ( $roomName )
        {
            $roomId = $this->model_common_addRoom( $roomName );
            //提示
            $messageData = array(
                'title' => '操作成功',
                'message' => "{$roomName}已添加，4秒后自动跳转回厅室管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'room', '', false ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['message'] = $messageBox;

            $this->get_common_section( $data );
            $this->load->view( 'admin/room', $data );
        }
        else
        {
            $this->room();
        }
    }
    
    /**
     * 修改厅室
     */
    public function roomedit()
    {
        if ( !$this->permission->chkPermission( 'room' ) ) $this->_error( config_item( 'base_url' ) );
        $roomId = intval( $this->input->post( 'id' ) );
        $roomData = $this->model_common_getRoomDataById( $roomId );
        if ( empty( $roomData ) )
        {
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => "4秒后自动跳转回厅室管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'room', '', false ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['message'] = $messageBox;

            $this->get_common_section( $data );
            $this->load->view( 'admin/room', $data );
        }
        else
        {
            $roomName = $this->_getRoomData( true );
            if ( $roomName )
            {
                $roomId = $this->model_common_updateRoomNameById( $roomName, $roomId );
                //提示
                $messageData = array(
                    'title' => '操作成功',
                    'message' => "4秒后自动跳转回厅室管理",
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => my_url( 'admin', 'room', '', false ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data['message'] = $messageBox;

                $this->get_common_section( $data );
                $this->load->view( 'admin/room', $data );
            }
            else
            {
                $this->room();
            }
        }
    }
    
    /**
     * 获取提交的厅室表单数据
     * @return string
     */
    private function _getRoomData( $isEdit = false )
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        if ( $isEdit )
        {
            $this->form_validation->set_rules( 'room', '区域', 'required' );
        }
        else
        {
            $this->form_validation->set_rules( 'room', '区域', 'required|callback_roomExist' );
        }
        if ( $this->form_validation->run() ) return $this->input->post( 'room' );
        return false;
    }
    
    /**
     * 管理链接
     * @param int $linkId 链接Id
     */
    public function link( $linkId = '' )
    {
        if ( !$this->permission->chkPermission( 'link' ) ) $this->_error( config_item( 'base_url' ) );
        
        $data = array( );

        if ( $linkId )
        {
            $selectedLinkData = $this->model_link_getById( $linkId );
            $data['selectedLinkData'] = $selectedLinkData;
        }
        $linkData = $this->model_link_getAll();
        $data['linkData'] = $linkData;

        $this->get_common_section( $data );
        $this->load->view( 'admin/link', $data );
    }
    
    /**
     * 添加链接
     */
    public function linkadd()
    {
        if ( !$this->permission->chkPermission( 'link' ) ) $this->_error( config_item( 'base_url' ) );
        $linkData = $this->_getLinkData();
        if ( $linkData )
        {
            $linkId = $this->model_link_add( $linkData );
            //提示
            $messageData = array(
                'title' => '操作成功',
                'message' => "{$linkData['name']}已添加，4秒后自动跳转回链接管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'link', '', false ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['message'] = $messageBox;

            $this->get_common_section( $data );
            $this->load->view( 'admin/link', $data );
        }
        else
        {
            $this->link();
        }
    }
    
    /**
     * 获取提交的链接表单数据
     * @return string
     */
    private function _getLinkData( $isEdit = false )
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        if( $isEdit )
        {
            $this->form_validation->set_rules( 'name', '链接名称', 'required' );
        }
        else
        {
            $this->form_validation->set_rules( 'name', '链接名称', 'required|callback_linkExist' );
        }
        $this->form_validation->set_rules( 'url', '网址', 'required' );
        if ( $this->form_validation->run() )
        {
            return array(
                'name' => $this->input->post('name'),
                'url' => prep_url( $this->input->post('url') ),
                'visible' => $this->input->post('visible'),
            );
        }
        return false;
    }
    
    public function linkExist( $linkName )
    {
        $linkData = $this->model_link_getDataByName( $linkName );
        if ( $linkData )
        {
            $this->form_validation->set_message( 'linkExist', '%s已经存在' );
            return false;
        }
        else
        {
            return true;
        }
    }
    
    /**
     * 修改链接
     */
    public function linkedit()
    {
        if ( !$this->permission->chkPermission( 'link' ) ) $this->_error( config_item( 'base_url' ) );
        $linkId = intval( $this->input->post( 'id' ) );
        $linkData = $this->model_link_getById( $linkId );
        if ( empty( $linkData ) )
        {
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => "4秒后自动跳转回链接管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'link', '', false ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['message'] = $messageBox;

            $this->get_common_section( $data );
            $this->load->view( 'admin/link', $data );
        }
        else
        {
            $linkData = $this->_getLinkData( true );
            if ( $linkData )
            {
                $linkId = $this->model_link_updateById( $linkData, $linkId );
                //提示
                $messageData = array(
                    'title' => '操作成功',
                    'message' => "4秒后自动跳转回链接管理",
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => my_url( 'admin', 'link', '', false ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data['message'] = $messageBox;

                $this->get_common_section( $data );
                $this->load->view( 'admin/link', $data );
            }
            else
            {
                $this->link();
            }
        }
    }
    
    /**
     * 删除指定的链接
     * @param int $linkId 链接ID
     */
    public function linkdelete( $linkId )
    {
        if ( !$this->permission->chkPermission( 'link' ) ) $this->_error( config_item( 'base_url' ) );
        $linkData = $this->model_link_getById( $linkId );
        if ( empty( $linkData ) )
        {
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => "4秒后自动跳转回链接管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'link', '', false ),
            );
        }
        else
        {
            $this->model_link_deleteById( $linkId );
            $messageData = array(
                'title' => '操作成功',
                'message' => "{$linkData['name']}链接已删除，4秒后自动跳转回链接管理",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'admin', 'link', '', false ),
            );
        }
        $messageBox = Modules::run( 'messageBox', $messageData );
        $data['message'] = $messageBox;
        $this->get_common_section( $data );
        $this->load->view( 'admin/link', $data );
    }
}